package com.sz.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    //字符串转换Date类型
    public static Date getDate() throws ParseException {

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date myDate = dateFormat.parse("2021-07-23 12:30:01");
        System.out.println(myDate);
        return myDate;
    }

    /**
     * 获得指定日期的后一天
     *
     * @return
     */
    public static Date getSpecifiedDayAfter(){
        Calendar c = Calendar.getInstance();
        Date date=null;
        date = new Date();
        //date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        c.setTime(date);
        int day=c.get(Calendar.DATE);
        c.set(Calendar.DATE,day+1);

        String dayAfter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
        Date d2=null;
        try {
            d2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dayAfter);

            System.out.println("d2 = " + d2);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return d2;
    }

    public static Date getMyDate(){
        Calendar c = Calendar.getInstance();
        Date date=null;
        date = new Date();
        //date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        c.setTime(date);
        int second=c.get(Calendar.SECOND);
        //c.set(Calendar.DATE,day+1);
        c.set(Calendar.SECOND,second+30);

        String dayAfter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
        Date d2=null;
        try {
            d2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dayAfter);

            System.out.println("d2 = " + d2);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return d2;
    }

    public static String getDateString(){
        Calendar c = Calendar.getInstance();
        Date date= new Date();
        String dayAfter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        return dayAfter;
    }


    public static void main(String[] args) {

        //System.out.println(getSpecifiedDayAfter());

        System.out.println(getDateString());
    }

}
